package com.hailou.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author hailou
 * @date 2013-12-9
 * @version 1.0
 */
public class DomainService {
	
	public static void main(String[] args){
		String domain = "xshell.cn";
		new DomainService().getDomain(domain);
	}

	public String  getDomain(String urlString){
		String urlIndexString = "http://whois.hichina.com/cgi-bin/whois?domain="+urlString;
		StringBuilder sb = new StringBuilder();
		String result = null;
		try {
			URL url = new URL(urlIndexString);
			
			HttpURLConnection huc = (HttpURLConnection)url.openConnection();
			huc.setDoInput(true);
			huc.setRequestMethod("GET");
			
			InputStream in = huc.getInputStream();
			InputStreamReader isr = new InputStreamReader(in,"gbk");
			BufferedReader br = new BufferedReader(isr);
			
			String flag = null;
			while((flag = br.readLine())!=null){
				sb.append(flag+"\n");
			}
			result = getSubString(sb.toString());
			br.close();
			isr.close();
			in.close();
			url = null;
			huc.disconnect();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch(IOException e){
			e.printStackTrace();
		} 
		return result;
	}
	public String getSubString(String urlString){
		StringBuilder sb = new StringBuilder(urlString);
		if(!"".equals(urlString)||urlString!=null){
			int index = sb.indexOf("<pre>");
			int end = sb.indexOf("</pre");
			urlString = sb.substring(index+5, end);
		}
		return urlString;
	}
}


 